package HibernateDAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Hibernate.HibernateUtil;
import beans.ItemLista;
import beans.Rodamiento;





public class HibernateItemsListaDAO {

	private static HibernateItemsListaDAO instancia = null;

	private static SessionFactory sf = null;

	public static HibernateItemsListaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateItemsListaDAO();
		} 
		return instancia;
	}

	public void eliminarItems(Rodamiento r) {
		Session s = sf.openSession();
		s.beginTransaction();
		//s.createQuery("delete from Listas l where l.items.")
		s.createQuery("delete from ItemLista il where il.rodamiento.idRodamiento = :id").setLong("id", r.getIdRodamiento());
		s.getTransaction().commit();
		s.close();
	}

	public void guardarUnItem(ItemLista IL){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(IL); 
		session.getTransaction().commit();
		session.close();		
	}
	
	public ItemLista obtenerItem(long iditem){
		Session session = sf.openSession();
		Query q = session.createQuery("FROM ItemLista IL WHERE IL.idItem = :id").setLong("id",iditem);
		ItemLista IT = (ItemLista) q.uniqueResult();
		session.close();
		return IT;
	}
}
